/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * The UI view of the survey manager
 */
qx.Class.define("bip.authorview.ui.SurveyManagerView", {
	extend : qx.ui.core.Widget,

	/*
	 * ****************************************************************************
	 * CONSTRUCTOR
	 * ****************************************************************************
	 */

	construct : function() {
		this.base(arguments);

		 // initialize the layout
    var layout = new qx.ui.layout.HBox(20);
    this._setLayout(layout);
    
    // Create forms
    this.__topicForm = new bip.authorview.forms.TopicPropertiesForm();
    this.__questionForm = new bip.authorview.forms.QuestionPropertiesForm();
    this.__surveyForm = new bip.authorview.forms.SurveyPropertiesForm();
    this.__newTopicForm = new bip.authorview.forms.NewTopicForm();
    this.__newQuestionForm = new bip.authorview.forms.NewQuestionForm();
    
		// Header
    this._createChildControl("tree");
    this._createChildControl("topic-properties");
    this._createChildControl("topic-add-question");
    this._createChildControl("question-properties");
    this._createChildControl("survey-properties");
    this._createChildControl("survey-add-topic");
    this.getChildControl("stack").setSelection([this.getChildControl("survey")]);
    
	},

	/*
	 * ****************************************************************************
	 * PROPERTIES
	 * ****************************************************************************
	 */

	properties : {
		// overridden
		appearance : {
			refine : true,
			init : "survey-manager"
		}
		
	},

	/*
	 * ****************************************************************************
	 * MEMBERS
	 * ****************************************************************************
	 */

	members : {
		__topicForm: null,
		__questionForm: null,
		__surveyForm: null,
		__newTopicForm: null,
		__newQuestionForm:null,
		
		
		/*
		 * ****************************************************************************
		 * UI
		 * ****************************************************************************
		 */
		// overridden
		_createChildControlImpl : function(id) {
			var control;
			switch (id) {
			
				case "tree":
					control = new qx.ui.tree.Tree();
					control.setWidth(200);
		      this._add(control);
					break;

				case "stack":
					control = new qx.ui.container.Stack();
					this._add(control);
					break;
				
				case "topic":
					control = new qx.ui.container.Composite();
					control.setLayout(new qx.ui.layout.VBox(5));
					this.getChildControl("stack").add(control);
					break;
					
				case "topic-properties":
					control = new bip.authorview.forms.ManageSurveyFormRenderer(this.__topicForm);
					this.getChildControl("topic").add(control);
					break;
					
				case "topic-add-question":
					control = new bip.authorview.forms.ManageSurveyFormRenderer(this.__newQuestionForm);
					this.getChildControl("topic").add(control);
					break;
					
				case "question-properties":
					control = new bip.authorview.forms.ManageSurveyFormRenderer(this.__questionForm);
					this.getChildControl("stack").add(control);
					break;
				
				case "survey":
					control = new qx.ui.container.Composite();
					control.setLayout(new qx.ui.layout.VBox(5));
					this.getChildControl("stack").add(control);
					break;
					
				case "survey-properties":
					control = new bip.authorview.forms.ManageSurveyFormRenderer(this.__surveyForm);
					this.getChildControl("survey").add(control);
					break;
					
				case "survey-add-topic":
					control = new bip.authorview.forms.ManageSurveyFormRenderer(this.__newTopicForm);
					this.getChildControl("survey").add(control);
					break;
          
			}

			return control || this.base(arguments, id);

		},
		
		setTopicFormModel: function(model) {
			this.__topicForm.getController().setModel(null); // Force model update
			this.__topicForm.getController().setModel(model.getTopic());
			this.__newQuestionForm.getController().setModel(model.getQuestionModel()); 
			this.getChildControl("stack").setSelection([this.getChildControl("topic")]);
		},
		
		setQuestionFormModel: function(model) {
			this.__questionForm.getController().setModel(null); // Force model update
			this.__questionForm.getController().setModel(model.getQuestion());
			this.getChildControl("stack").setSelection([this.getChildControl("question-properties")]);
		},
		
		setSurveyFormModel: function(model, topicModel) {
			this.__surveyForm.getController().setModel(model); 
			this.__newTopicForm.getController().setModel(model.getTopicModel()); 
			this.getChildControl("tree").getRoot().setOpen(true);
			this.getChildControl("stack").setSelection([this.getChildControl("survey")]);
		},
		
		reset: function() {
			this.__topicForm.renewController();
			this.__newTopicForm.renewController();
			this.__questionForm.renewController();
			this.__newQuestionForm.renewController();
			this.__surveyForm.renewController();
			this.getChildControl("stack").setSelection([this.getChildControl("survey")]);
		}
	},

	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects("__topicForm","__newTopicForm","__questionForm","__newQuestionForm","__surveyForm");
	}
});
